<template>
  <div class="checkup-entry-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>新增健康体检</span>
        </div>
      </template>
      <el-form :model="formData" :rules="rules" ref="checkupForm" label-width="160px">
        <el-collapse v-model="activeCollapseNames">
          <!-- 基本信息 -->
          <el-collapse-item title="基本信息" name="1">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="档案ID" prop="healthExamination.da_id">
                  <el-input v-model="formData.healthExamination.da_id" placeholder="请输入档案ID"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="体检日期" prop="healthExamination.tjrq">
                  <el-date-picker v-model="formData.healthExamination.tjrq" type="date" placeholder="选择日期" style="width: 100%;"></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="责任医生" prop="healthExamination.zrys_id">
                  <el-input v-model="formData.healthExamination.zrys_id" placeholder="请输入责任医生ID"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>

          <!-- 症状 -->
          <el-collapse-item title="症状" name="2">
            <el-form-item label="症状" prop="healthExamination.zzbm">
              <el-checkbox-group v-model="formData.healthExamination.zzbm">
                <el-checkbox label="1">无症状</el-checkbox>
                <el-checkbox label="2">头痛</el-checkbox>
                <el-checkbox label="3">头晕</el-checkbox>
                <el-checkbox label="4">心悸</el-checkbox>
                <el-checkbox label="5">胸闷</el-checkbox>
                <el-checkbox label="6">胸痛</el-checkbox>
                <el-checkbox label="7">慢性咳嗽</el-checkbox>
                <el-checkbox label="8">咳痰</el-checkbox>
                <el-checkbox label="9">呼吸困难</el-checkbox>
                <el-checkbox label="10">多饮</el-checkbox>
                <el-checkbox label="11">多尿</el-checkbox>
                <el-checkbox label="12">体重下降</el-checkbox>
                <el-checkbox label="13">乏力</el-checkbox>
                <el-checkbox label="14">关节肿痛</el-checkbox>
                <el-checkbox label="15">视力模糊</el-checkbox>
                <el-checkbox label="16">手脚麻木</el-checkbox>
                <el-checkbox label="17">尿急</el-checkbox>
                <el-checkbox label="18">尿痛</el-checkbox>
                <el-checkbox label="19">便秘</el-checkbox>
                <el-checkbox label="20">腹泻</el-checkbox>
                <el-checkbox label="21">恶心呕吐</el-checkbox>
                <el-checkbox label="22">眼花</el-checkbox>
                <el-checkbox label="23">耳鸣</el-checkbox>
                <el-checkbox label="24">乳房胀痛</el-checkbox>
                <el-checkbox label="25">其他</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="其他症状描述" v-if="formData.healthExamination.zzbm.includes('25')">
              <el-input v-model="formData.healthExamination.zzqt" type="textarea" placeholder="请输入其他症状描述"></el-input>
            </el-form-item>
          </el-collapse-item>

          <!-- 一般状况 -->
          <el-collapse-item title="一般状况" name="3">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="体温(℃)" prop="healthExamination.tw">
                  <el-input v-model="formData.healthExamination.tw"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="脉率(次/分钟)" prop="healthExamination.ml">
                  <el-input v-model="formData.healthExamination.ml"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="呼吸频率(次/分钟)" prop="healthExamination.hxpl">
                  <el-input v-model="formData.healthExamination.hxpl"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="血压左侧收缩压" prop="healthExamination.xyssyz">
                  <el-input v-model="formData.healthExamination.xyssyz" placeholder="mmHg"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="血压左侧舒张压" prop="healthExamination.xyszyz">
                  <el-input v-model="formData.healthExamination.xyszyz" placeholder="mmHg"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                 <el-form-item label="血压右侧收缩压" prop="healthExamination.xyssyy">
                  <el-input v-model="formData.healthExamination.xyssyy" placeholder="mmHg"></el-input>
                </el-form-item>
              </el-col>
               <el-col :span="8">
                <el-form-item label="血压右侧舒张压" prop="healthExamination.xyszyy">
                  <el-input v-model="formData.healthExamination.xyszyy" placeholder="mmHg"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="身高(cm)" prop="healthExamination.sg">
                  <el-input v-model="formData.healthExamination.sg" @input="calculateBmi"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="体重(kg)" prop="healthExamination.tz">
                  <el-input v-model="formData.healthExamination.tz" @input="calculateBmi"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="腰围(cm)" prop="healthExamination.yw">
                  <el-input v-model="formData.healthExamination.yw"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="体质指数(BMI)" prop="healthExamination.tzzs">
                  <el-input v-model="formData.healthExamination.tzzs" disabled></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <hr/>
            <h4>老年人评估</h4>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-form-item label="健康状态自我评估" prop="healthExamination.lnrjkztzp">
                        <el-select v-model="formData.healthExamination.lnrjkztzp" placeholder="请选择">
                            <el-option label="满意" value="1"></el-option>
                            <el-option label="基本满意" value="2"></el-option>
                            <el-option label="说不清楚" value="3"></el-option>
                            <el-option label="不太满意" value="4"></el-option>
                            <el-option label="不满意" value="5"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="生活自理能力自我评估" prop="healthExamination.lnrzlnlzp">
                         <el-select v-model="formData.healthExamination.lnrzlnlzp" placeholder="请选择">
                            <el-option label="可自理(0-3分)" value="1"></el-option>
                            <el-option label="轻度依赖(4-8分)" value="2"></el-option>
                            <el-option label="中度依赖(9-18分)" value="3"></el-option>
                            <el-option label="不能自理(>=19分)" value="4"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                 <el-col :span="8">
                    <el-form-item label="认知能力" prop="healthExamination.lnrrzgn">
                        <el-select v-model="formData.healthExamination.lnrrzgn" placeholder="请选择">
                            <el-option label="粗筛阴性" value="1"></el-option>
                            <el-option label="粗筛阳性" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="认知功能总分" prop="healthExamination.rzgnzf">
                        <el-input v-model="formData.healthExamination.rzgnzf"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="情感状态" prop="healthExamination.lnrqgzt">
                        <el-select v-model="formData.healthExamination.lnrqgzt" placeholder="请选择">
                            <el-option label="粗筛阴性" value="1"></el-option>
                            <el-option label="粗筛阳性" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="情感状态总分" prop="healthExamination.qgztzf">
                        <el-input v-model="formData.healthExamination.qgztzf"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
          </el-collapse-item>

          <!-- 生活方式 -->
          <el-collapse-item title="生活方式" name="4">
             <el-row :gutter="20">
                <el-col :span="8">
                    <el-form-item label="锻炼频率" prop="healthExamination.tydlpl">
                        <el-select v-model="formData.healthExamination.tydlpl" placeholder="请选择">
                            <el-option label="每天" value="1"></el-option>
                            <el-option label="每周一次以上" value="2"></el-option>
                            <el-option label="偶尔" value="3"></el-option>
                            <el-option label="不锻炼" value="4"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="每次锻炼时间(分钟)" prop="healthExamination.mcdlsj">
                        <el-input v-model="formData.healthExamination.mcdlsj"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="坚持锻炼时间(年)" prop="healthExamination.jcdlsj">
                        <el-input v-model="formData.healthExamination.jcdlsj"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="锻炼方式" prop="healthExamination.dlfs">
                        <el-input v-model="formData.healthExamination.dlfs"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="饮食习惯" prop="healthExamination.ysxg">
                        <el-checkbox-group v-model="formData.healthExamination.ysxg">
                            <el-checkbox label="1">荤素均衡</el-checkbox>
                            <el-checkbox label="2">荤食为主</el-checkbox>
                            <el-checkbox label="3">素食为主</el-checkbox>
                            <el-checkbox label="4">嗜盐</el-checkbox>
                            <el-checkbox label="5">嗜油</el-checkbox>
                            <el-checkbox label="6">嗜糖</el-checkbox>
                        </el-checkbox-group>
                    </el-form-item>
                </el-col>
            </el-row>
            <hr/>
            <h4>吸烟情况</h4>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-form-item label="吸烟状况" prop="healthExamination.xyzk">
                        <el-select v-model="formData.healthExamination.xyzk" placeholder="请选择">
                            <el-option label="从不吸烟" value="1"></el-option>
                            <el-option label="已戒烟" value="2"></el-option>
                            <el-option label="吸烟" value="3"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="formData.healthExamination.xyzk === '3'">
                    <el-form-item label="日吸烟量(支)" prop="healthExamination.rxyl">
                        <el-input v-model="formData.healthExamination.rxyl"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="formData.healthExamination.xyzk === '3'">
                    <el-form-item label="开始吸烟年龄(岁)" prop="healthExamination.ksxynl">
                        <el-input v-model="formData.healthExamination.ksxynl"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="formData.healthExamination.xyzk === '2'">
                    <el-form-item label="戒烟年龄(岁)" prop="healthExamination.jynl">
                        <el-input v-model="formData.healthExamination.jynl"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <hr/>
            <h4>饮酒情况</h4>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-form-item label="饮酒频率" prop="healthExamination.yjpl">
                        <el-select v-model="formData.healthExamination.yjpl" placeholder="请选择">
                            <el-option label="从不" value="1"></el-option>
                            <el-option label="偶尔" value="2"></el-option>
                            <el-option label="经常" value="3"></el-option>
                            <el-option label="每天" value="4"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="formData.healthExamination.yjpl !== '1'">
                    <el-form-item label="日饮酒量(两)" prop="healthExamination.ryjl">
                        <el-input v-model="formData.healthExamination.ryjl"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="formData.healthExamination.yjpl !== '1'">
                    <el-form-item label="是否戒酒" prop="healthExamination.sfjj">
                        <el-select v-model="formData.healthExamination.sfjj" placeholder="请选择">
                            <el-option label="未戒酒" value="1"></el-option>
                            <el-option label="已戒酒" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="formData.healthExamination.sfjj === '2'">
                    <el-form-item label="戒酒年龄(岁)" prop="healthExamination.jjnl">
                        <el-input v-model="formData.healthExamination.jjnl"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="formData.healthExamination.yjpl !== '1'">
                    <el-form-item label="开始饮酒年龄(岁)" prop="healthExamination.ksyjnl">
                        <el-input v-model="formData.healthExamination.ksyjnl"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="formData.healthExamination.yjpl !== '1'">
                    <el-form-item label="近一年是否醉酒" prop="healthExamination.jynnsfzj">
                        <el-radio-group v-model="formData.healthExamination.jynnsfzj">
                            <el-radio label="1">是</el-radio>
                            <el-radio label="2">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if="formData.healthExamination.yjpl !== '1'">
                    <el-form-item label="饮酒种类" prop="healthExamination.yjzl">
                        <el-checkbox-group v-model="formData.healthExamination.yjzl">
                            <el-checkbox label="1">白酒</el-checkbox>
                            <el-checkbox label="2">啤酒</el-checkbox>
                            <el-checkbox label="3">红酒</el-checkbox>
                            <el-checkbox label="4">黄酒</el-checkbox>
                            <el-checkbox label="5">其他</el-checkbox>
                        </el-checkbox-group>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if="formData.healthExamination.yjzl && formData.healthExamination.yjzl.includes('5')">
                    <el-form-item label="其他饮酒种类说明">
                        <el-input v-model="formData.healthExamination.yjzlqtmc" type="textarea"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
          </el-collapse-item>

          <!-- 职业病危害因素接触史 -->
          <el-collapse-item title="职业病危害因素接触史" name="5">
            <el-form-item label="有无接触史" prop="healthExamination.zybwhysjcs">
                <el-radio-group v-model="formData.healthExamination.zybwhysjcs">
                    <el-radio label="1">无</el-radio>
                    <el-radio label="2">有</el-radio>
                </el-radio-group>
            </el-form-item>
            <div v-if="formData.healthExamination.zybwhysjcs === '2'">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="工种" prop="healthExamination.gzzl">
                            <el-input v-model="formData.healthExamination.gzzl"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="从业时间(年)" prop="healthExamination.cysj">
                            <el-input v-model="formData.healthExamination.cysj"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <p><strong>毒物-粉尘</strong></p>
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="粉尘" prop="healthExamination.dwfc">
                            <el-input v-model="formData.healthExamination.dwfc"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="防护措施" prop="healthExamination.dwfcbhcs">
                            <el-radio-group v-model="formData.healthExamination.dwfcbhcs">
                                <el-radio label="1">有</el-radio>
                                <el-radio label="2">无</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="formData.healthExamination.dwfcbhcs === '1'">
                        <el-form-item label="防护措施说明" prop="healthExamination.dwfcfhcsmc">
                            <el-input v-model="formData.healthExamination.dwfcfhcsmc"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <p><strong>毒物-放射物质</strong></p>
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="放射物质" prop="healthExamination.dwfswz">
                            <el-input v-model="formData.healthExamination.dwfswz"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="防护措施" prop="healthExamination.dwfswzbhcs">
                            <el-radio-group v-model="formData.healthExamination.dwfswzbhcs">
                                <el-radio label="1">有</el-radio>
                                <el-radio label="2">无</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="formData.healthExamination.dwfswzbhcs === '1'">
                        <el-form-item label="防护措施说明" prop="healthExamination.dwfswzbhcsmc">
                            <el-input v-model="formData.healthExamination.dwfswzbhcsmc"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <p><strong>毒物-物理因素</strong></p>
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="物理因素" prop="healthExamination.dwwlys">
                            <el-input v-model="formData.healthExamination.dwwlys"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="防护措施" prop="healthExamination.dwwlysbhcs">
                            <el-radio-group v-model="formData.healthExamination.dwwlysbhcs">
                                <el-radio label="1">有</el-radio>
                                <el-radio label="2">无</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="formData.healthExamination.dwwlysbhcs === '1'">
                        <el-form-item label="防护措施说明" prop="healthExamination.dwwlysbhcsmc">
                            <el-input v-model="formData.healthExamination.dwwlysbhcsmc"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <p><strong>毒物-化学物质</strong></p>
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="化学物质" prop="healthExamination.dwhxwz">
                            <el-input v-model="formData.healthExamination.dwhxwz"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="防护措施" prop="healthExamination.dwhxwzbhcs">
                            <el-radio-group v-model="formData.healthExamination.dwhxwzbhcs">
                                <el-radio label="1">有</el-radio>
                                <el-radio label="2">无</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="formData.healthExamination.dwhxwzbhcs === '1'">
                        <el-form-item label="防护措施说明" prop="healthExamination.dwhxwzbhcsmc">
                            <el-input v-model="formData.healthExamination.dwhxwzbhcsmc"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <p><strong>毒物-其他</strong></p>
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="其他" prop="healthExamination.dwqt">
                            <el-input v-model="formData.healthExamination.dwqt"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="防护措施" prop="healthExamination.dwqtbhcs">
                            <el-radio-group v-model="formData.healthExamination.dwqtbhcs">
                                <el-radio label="1">有</el-radio>
                                <el-radio label="2">无</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="formData.healthExamination.dwqtbhcs === '1'">
                        <el-form-item label="防护措施说明" prop="healthExamination.dwqtbhcsmc">
                            <el-input v-model="formData.healthExamination.dwqtbhcsmc"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
          </el-collapse-item>

          <!-- 查体 -->
          <el-collapse-item title="查体" name="6">
            <!-- ... 此处省略查体部分的详细字段，将在后续步骤中添加 ... -->
          </el-collapse-item>

          <!-- 辅助检查 -->
          <el-collapse-item title="辅助检查" name="7">
            <!-- ... 此处省略辅助检查部分的详细字段，将在后续步骤中添加 ... -->
          </el-collapse-item>

          <!-- 现存主要健康问题 -->
          <el-collapse-item title="现存主要健康问题" name="8">
            <!-- ... 此处省略现存主要健康问题部分的详细字段，将在后续步骤中添加 ... -->
          </el-collapse-item>

          <!-- 住院史 -->
          <el-collapse-item title="住院史" name="9">
            <div v-for="(item, index) in formData.hospitalizationHistory" :key="index" class="dynamic-item">
                <el-row :gutter="10">
                    <el-col :span="5"><el-form-item label="入院日期"><el-date-picker v-model="item.ryrq" type="date" style="width:100%"></el-date-picker></el-form-item></el-col>
                    <el-col :span="5"><el-form-item label="出院日期"><el-date-picker v-model="item.cyrq" type="date" style="width:100%"></el-date-picker></el-form-item></el-col>
                    <el-col :span="5"><el-form-item label="医疗机构"><el-input v-model="item.yyjgbm" placeholder="医疗机构名称"></el-input></el-form-item></el-col>
                    <el-col :span="5"><el-form-item label="原因/病案号"><el-input v-model="item.jbbm" placeholder="原因/主要病症"></el-input></el-form-item></el-col>
                    <el-col :span="2"><el-button type="danger" @click="removeHistory('hospitalizationHistory', index)">删除</el-button></el-col>
                </el-row>
            </div>
            <el-button @click="addHistory('hospitalizationHistory')">添加住院史</el-button>
          </el-collapse-item>

          <!-- 家庭病床史 -->
          <el-collapse-item title="家庭病床史" name="10">
            <div v-for="(item, index) in formData.familyBedHistory" :key="index" class="dynamic-item">
                <el-row :gutter="10">
                    <el-col :span="5"><el-form-item label="建床日期"><el-date-picker v-model="item.jcrq" type="date" style="width:100%"></el-date-picker></el-form-item></el-col>
                    <el-col :span="5"><el-form-item label="撤床日期"><el-date-picker v-model="item.ccrq" type="date" style="width:100%"></el-date-picker></el-form-item></el-col>
                    <el-col :span="5"><el-form-item label="医疗机构"><el-input v-model="item.yyjgbm" placeholder="医疗机构名称"></el-input></el-form-item></el-col>
                    <el-col :span="5"><el-form-item label="原因/病案号"><el-input v-model="item.jbbm" placeholder="原因/主要病症"></el-input></el-form-item></el-col>
                    <el-col :span="2"><el-button type="danger" @click="removeHistory('familyBedHistory', index)">删除</el-button></el-col>
                </el-row>
            </div>
            <el-button @click="addHistory('familyBedHistory')">添加家庭病床史</el-button>
          </el-collapse-item>

          <!-- 主要用药情况 -->
          <el-collapse-item title="主要用药情况" name="11">
             <div v-for="(med, index) in formData.medication" :key="index" class="dynamic-item">
                <el-row :gutter="10">
                    <el-col :span="5"><el-form-item label="药品名称"><el-input v-model="med.ypmc" placeholder="药品名称"></el-input></el-form-item></el-col>
                    <el-col :span="4"><el-form-item label="用法"><el-input v-model="med.yf" placeholder="用法"></el-input></el-form-item></el-col>
                    <el-col :span="3"><el-form-item label="用量"><el-input v-model="med.yl" placeholder="用量"></el-input></el-form-item></el-col>
                    <el-col :span="3"><el-form-item label="单位"><el-input v-model="med.yldw" placeholder="单位"></el-input></el-form-item></el-col>
                    <el-col :span="5"><el-form-item label="服药依从性"><el-input v-model="med.ypfyycx" placeholder="例如: 规律/间断/不服药"></el-input></el-form-item></el-col>
                    <el-col :span="2"><el-button type="danger" @click="removeHistory('medication', index)">删除</el-button></el-col>
                </el-row>
            </div>
            <el-button @click="addHistory('medication')">添加用药</el-button>
          </el-collapse-item>

          <!-- 非免疫规划预防接种史 -->
          <el-collapse-item title="非免疫规划预防接种史" name="12">
             <div v-for="(item, index) in formData.vaccinationHistory" :key="index" class="dynamic-item">
                <el-row :gutter="10">
                    <el-col :span="8"><el-form-item label="接种名称"><el-input v-model="item.jzmc" placeholder="疫苗名称"></el-input></el-form-item></el-col>
                    <el-col :span="8"><el-form-item label="接种日期"><el-date-picker v-model="item.jzrq" type="date" style="width:100%"></el-date-picker></el-form-item></el-col>
                    <el-col :span="6"><el-form-item label="接种机构"><el-input v-model="item.jzjg" placeholder="接种机构名称"></el-input></el-form-item></el-col>
                    <el-col :span="2"><el-button type="danger" @click="removeHistory('vaccinationHistory', index)">删除</el-button></el-col>
                </el-row>
            </div>
            <el-button @click="addHistory('vaccinationHistory')">添加接种史</el-button>
          </el-collapse-item>

          <!-- 健康评价 -->
          <el-collapse-item title="健康评价与指导" name="13">
            <!-- ... 此处省略健康评价与指导部分的详细字段，将在后续步骤中添加 ... -->
          </el-collapse-item>

        </el-collapse>

        <el-form-item style="margin-top: 20px;">
          <el-button type="primary" @click="saveCheckup">保存</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue';
import { ElMessage } from 'element-plus';
// import checkupApi from '@/api/checkup'; // 假设你的API在这里

export default {
  name: 'CheckupEntry',
  setup() {
    const checkupForm = ref(null);
    const activeCollapseNames = ref(['1', '2', '3', '4', '5', '9', '10', '11', '12', '13']);

    const state = reactive({
      formData: {
        healthExamination: {
          da_id: '',
          tjrq: '',
          zrys_id: '',
          zzbm: [],
          zzqt: '',
          tw: '',
          ml: '',
          hxpl: '',
          xyssyz: '',
          xyszyz: '',
          xyssyy: '',
          xyszyy: '',
          sg: '',
          tz: '',
          yw: '',
          tzzs: '',
          lnrjkztzp: '',
          lnrzlnlzp: '',
          lnrrzgn: '',
          rzgnzf: '',
          lnrqgzt: '',
          qgztzf: '',
          tydlpl: '',
          mcdlsj: '',
          jcdlsj: '',
          dlfs: '',
          ysxg: [],
          xyzk: '',
          rxyl: '',
          ksxynl: '',
          jynl: '',
          yjpl: '',
          ryjl: '',
          sfjj: '',
          jjnl: '',
          ksyjnl: '',
          jynnsfzj: '',
          yjzl: [],
          yjzlqtmc: '',
          zybwhysjcs: '1',
          gzzl: '',
          cysj: '',
          dwfc: '',
          dwfcbhcs: '2',
          dwfcfhcsmc: '',
          dwfswz: '',
          dwfswzbhcs: '2',
          dwfswzbhcsmc: '',
          dwwlys: '',
          dwwlysbhcs: '2',
          dwwlysbhcsmc: '',
          dwhxwz: '',
          dwhxwzbhcs: '2',
          dwhxwzbhcsmc: '',
          dwqt: '',
          dwqtbhcs: '2',
          dwqtbhcsmc: '',
        },
        ancillaryExam: {},
        medication: [],
        hospitalizationHistory: [],
        familyBedHistory: [],
        vaccinationHistory: [],
        healthAbnormalities: [],
        adlAssessment: {},
      },
      rules: {
        'healthExamination.da_id': [{ required: true, message: '请输入档案ID', trigger: 'blur' }],
        'healthExamination.tjrq': [{ required: true, message: '请选择体检日期', trigger: 'change' }],
        'healthExamination.zrys_id': [{ required: true, message: '请输入责任医生ID', trigger: 'blur' }],
      },
    });

    const calculateBmi = () => {
      const height = parseFloat(state.formData.healthExamination.sg);
      const weight = parseFloat(state.formData.healthExamination.tz);
      if (height > 0 && weight > 0) {
        const bmi = weight / ((height / 100) * (height / 100));
        state.formData.healthExamination.tzzs = bmi.toFixed(2);
      }
    };

    const historyTemplates = {
        hospitalizationHistory: { ryrq: '', cyrq: '', yyjgbm: '', jbbm: ''},
        familyBedHistory: { jcrq: '', ccrq: '', yyjgbm: '', jbbm: ''},
        medication: { ypmc: '', yf: '', yl: '', yldw: '', ypfyycx: '' },
        vaccinationHistory: { jzmc: '', jzrq: '', jzjg: ''}
    }

    const addHistory = (type) => {
        if(state.formData[type]){
            state.formData[type].push({...historyTemplates[type]});
        }
    };

    const removeHistory = (type, index) => {
        if(state.formData[type]){
            state.formData[type].splice(index, 1);
        }
    };

    const saveCheckup = () => {
      checkupForm.value.validate(async (valid) => {
        if (valid) {
          try {
            const postData = JSON.parse(JSON.stringify(state.formData));
            
            // 将数组转换为逗号分隔的字符串
            postData.healthExamination.zzbm = postData.healthExamination.zzbm.join(',');
            postData.healthExamination.ysxg = postData.healthExamination.ysxg.join(',');
            postData.healthExamination.yjzl = postData.healthExamination.yjzl.join(',');
            // ... 其他需要转换的字段

            console.log('Submitting data:', postData);
            // await checkupApi.createCheckup(postData);
            ElMessage.success('体检记录保存成功 (模拟)');
            // resetForm();
          } catch (error) {
            console.error('保存失败:', error);
            ElMessage.error('体检记录保存失败');
          }
        } else {
          ElMessage.error('请检查表单填写是否正确');
          return false;
        }
      });
    };

    const resetForm = () => {
      checkupForm.value.resetFields();
      state.formData.medication = [];
      state.formData.hospitalizationHistory = [];
      state.formData.familyBedHistory = [];
      state.formData.vaccinationHistory = [];
      // ... 重置其他数据
    };

    return {
      ...toRefs(state),
      checkupForm,
      activeCollapseNames,
      calculateBmi,
      addHistory,
      removeHistory,
      saveCheckup,
      resetForm,
    };
  },
};
</script>

<style scoped>
.checkup-entry-container {
  padding: 20px;
}
.card-header {
  font-size: 18px;
  font-weight: bold;
}
.dynamic-item {
  margin-bottom: 15px;
  padding: 10px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}
.el-row {
    margin-bottom: 0;
}
.dynamic-item .el-form-item {
    margin-bottom: 0;
}
hr {
    border: 0;
    height: 1px;
    background-color: #dcdfe6;
    margin: 20px 0;
}
h4, p {
    margin-bottom: 15px;
    font-weight: bold;
}
</style>